import styles from '../styles.less';
import {Dropdown, Pagination, Space, Table, Tag} from 'antd';
import {useCallback, useEffect} from 'react';
import {connect} from 'umi';
import {nowrapColumns} from '@/utils/utils';
import {DownOutlined} from "@ant-design/icons";

const DataTable = (props) => {

  const {dispatch, dataTable, loading} = props;
  const {tablePayload, setTablePayload, setDetail} = props.data;

  /* 获取列表 */
  const fetchList = useCallback((payload) => {
    dispatch({
      type: 'POrderPolypay/fetchList',
      payload: payload,
    });
  }, [dispatch]);

  /* 查询监听 */
  useEffect(() => {
    fetchList(tablePayload);
  }, [fetchList, tablePayload]);

  /* 查看详情 */
  function detail(e, record) {
    e.preventDefault();
    setDetail({
      visible: true,
      title: '查看详情',
      data: record,
    });
    return false;
  }

  const columns = [
    {
      title: '序号',
      align: 'center',
      width: 70,
      fixed: 'left',
      render: ({}, {}, index) => (
        <span>
          {(tablePayload.pageNum - 1) * tablePayload.pageSize + index + 1}
        </span>
      ),
    },
    {
      title: '商家名称',
      dataIndex: 'mchName',
      align: 'center',
      render: (_, record) => {
        return (
          <span>{record.mchName ? record.mchName: '-'}</span>
        );
      },
    },
    {
      title: '订单编号',
      dataIndex: 'orderNo',
      align: 'center',
      render: (_, record) => {
        return (
          <span>{record.orderNo ? record.orderNo : '-'}</span>
        );
      },
    },
    {
      title: '原始金额',
      dataIndex: 'amount',
      align: 'center',
      ellipsis: true,
      render: (_, record) => {
        return (
          <span>{(record.amount / 100).toFixed(2)}</span>
        );
      },
    },
    {
      title: '优惠金额',
      dataIndex: 'couponAmount',
      align: 'center',
      ellipsis: true,
      render: (_, record) => {
        return (
          <span>{(record.couponAmount / 100).toFixed(2)}</span>
        );
      },
    },
    {
      title: '余额抵扣',
      dataIndex: 'balanceAmount',
      align: 'center',
      ellipsis: true,
      render: (_, record) => {
        return (
          <span style={{color: 'green'}}>{(record.balanceAmount / 100).toFixed(2)}</span>
        );
      },
    },
    {
      title: '支付金额',
      dataIndex: 'actualAmount',
      align: 'center',
      ellipsis: true,
      render: (_, record) => {
        return (
          <span style={{color: 'orangered'}}>{(record.actualAmount / 100).toFixed(2)}</span>
        );
      },
    },
    {
      title: '支付渠道',
      dataIndex: 'payChannelName',
      align: 'center',
      ellipsis: true,
      render: (_, record) => {
        return (
          <span>{record.payChannelName ? record.payChannelName : '-'}</span>
        );
      },
    },
    {
      title: '订单状态',
      dataIndex: 'status',
      align: 'center',
      ellipsis: true,
      render: (_, record) => {
        return (
          <Tag color={record.status === 1 ? 'geekblue' : 'volcano'}>
            {record.status === 1 ? '已支付' : '未支付'}
          </Tag>
        );
      },
    },
    {
      title: '付款时间',
      dataIndex: 'timePaid',
      align: 'center',
      ellipsis: true,
      render: (_, record) => {
        return (
          <span>{record.timePaid ? record.timePaid : '-'}</span>
        );
      },
    },
    {
      title: '创建时间',
      dataIndex: 'createTime',
      align: 'center',
      ellipsis: true,
    },
    {
      title: '备注信息',
      dataIndex: 'remark',
      align: 'center',
      ellipsis: true,
      render: (_, record) => {
        return (
          <span>{record.remark ? record.remark : '-'}</span>
        );
      },
    },
    {
      title: '操作',
      dataIndex: 'option',
      align: 'center',
      width: 100,
      fixed: 'right',
      render: (_, record) => {
        return (
          <div style={{display: 'flex', textAlign: 'center', justifyContent: 'center'}}>
            <Space size='small'>
              <a onClick={e => detail(e, record)}>详情</a>
            </Space>
          </div>
        )
      },
    },
  ];

  return (
    <>
      <div className={styles.myDataTable}>
        <Table
          className={styles.myTable} bordered={false} size={'middle'} columns={nowrapColumns(columns, dataTable.list)}
          dataSource={dataTable.list} rowKey={'id'}
          pagination={false} scroll={{y: '100%'}} loading={loading}
        />
      </div>
      <Pagination
        pageSize={tablePayload.pageSize} current={tablePayload.pageNum} total={dataTable.total}
        className={styles.myPagination} size={'default'} hideOnSinglePage={false}
        showTotal={(total) => {
          return `共 ${total} 条记录`;
        }}
        pageSizeOptions={[15, 30, 60, 120]}
        showSizeChanger showQuickJumper={true}
        onChange={(page, pageSize) => {
          setTablePayload({...tablePayload, pageNum: page, pageSize: pageSize});
        }}
      />
    </>

  );
};

export default connect(({loading, POrderPolypay}) => {
  return {
    loading: loading.effects['POrderPolypay/fetchList'],
    dataTable: POrderPolypay.dataTable,
  };
})(DataTable);
